<template>
  <div class="app-page">
    <!-- 标题 -->
    <div class="app-header">
      <div class="app-title">
        <i :class="$route.meta.icon"></i>  {{ $route.meta.title }}
      </div>
      <div class="app-tools">
        <el-button icon="fa fa-filter" size="small" @click="showSearch = !showSearch">
          {{ showSearch?'隐藏':'查询' }}
        </el-button>
        <el-button icon="fa fa-plus" size="small" @click="goRouterView('/add')">
          添加
        </el-button>
        <el-button :disabled="multipleSelection.length===0" icon="el-icon-delete" size="small" @click="delRows(multipleSelection)">
          批量删除
        </el-button>
      </div>
    </div>
    <!-- 内容 -->
    <div class="app-body">
      <!-- 搜索 -->
      <el-form
        v-if="showSearch"
        ref="form"
        :model="searchForm"
        size="small"
        inline
        class="app-search-form"
      >
        <el-form-item label="类型">
          <el-select v-model="searchForm.name01" placeholder="请选择" clearable style="width:100px">
            <el-option label="全部" value="" />
          </el-select>
        </el-form-item><el-form-item label="项目名称">
          <el-input v-model="searchForm.name02" placeholder="输入项目名称" clearable style="width:200px"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="query(searchForm)">
            查询
          </el-button>
          <el-button @click="resetForm">
            重置
          </el-button>
        </el-form-item>
      </el-form>
      <!-- 表格 -->
      <div v-auto-height class="app-table-box">
        <el-table
          v-loading="tableLoading"
          :data="tableData"
          border
          height="100%"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <!-- <el-table-column type="index" width="70" align="center" label="序号" /> -->
          <el-table-column prop="field01" align="center" label="项目名称" show-overflow-tooltip></el-table-column>
          <el-table-column prop="field02" align="center" label="类型" show-overflow-tooltip></el-table-column>
          <el-table-column prop="field03" align="center" label="计划开始时间" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.field03 }}
            </template>
          </el-table-column>
          <el-table-column prop="field04" align="center" label="当前阶段" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" label="操作" width="150">
            <template slot-scope="scope">
              <el-button type="text" class="primary-color" size="medium" @click="goRouterView(`/edit?uuid=${scope.row.uuid}`)">
                编辑
              </el-button>
              <el-button type="text" class="danger-color" size="medium" @click="delRows([scope.row.uuid])">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>
<script>
import { remove, getData } from '@/mock/index'

// 搜索分页表
import tableSearchMixin from '@/mixins/tableSearch.js'
export default {
  name: 'ExampleTableSearch',
  mixins: [tableSearchMixin],
  data () {
    return {
      // 多选
      multipleSelection: []
    }
  },
  created () {
    this.getTableData()
  },
  methods: {
    // 选择变化
    handleSelectionChange (val) {
      this.multipleSelection = val.map(ele => ele.uuid)
    },
    // 获取列表
    async getTableData (searchForm = this.searchForm) {
      // 模拟表格数据
      const tableData = [
        {
          field01: '项目A',
          field02: '',
          field03: '2019-3-12',
          field04: '论证阶段',
          field05: '210000',
          field06: '',
          uuid: 'uuid'
        }
      ]
      this.tableLoading = true
      const res = await getData(searchForm, tableData)
      this.tableLoading = false
      if (res.code === '0000') {
        this.tableData = res.data
      }
    },
    // 删除
    delRows (ids) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await remove({ ids })
        if (res.code === '0000') {
          this.$message({
            type: 'success',
            message: res.message
          })
          // 更新表格
          this.getTableData()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
</style>
